//
//  YDVerticalKTimeLineView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/7.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDVerticalKTimeLineView.h"
#import "UIView+Multiple.h"
#import "MulTipleViewSelectedView.h"

//分时图
#import "StockDataServerTool.h"
#import "YDVerticalStockTimeLineView.h"
#import "StockDataServerTool.h"
#import "StockTimeLineModel.h"

//K图
#import "YDVerticalStockBlendKView.h"
#import "StockKLineModel.h"

typedef enum
{
    TimeLine = 1,
    DayKLine,
    WeekKLine,
    MonthKLine
}StockDataLineType;

@interface YDVerticalKTimeLineView()<UIScrollViewDelegate , MulTipleViewSelectedViewDelegate , SYVerticalStockBlendKViewDelegate , SYVerticalStockTimeLineViewDelegate>

@property (nonatomic , assign) StockDataLineType lineType;
@property (nonatomic , assign) NSInteger KDataType;//1表示天K，2表示周K，3表示月K
@property (nonatomic , assign) NSInteger dayNumbers;//加载的数据条数 日K月KZ周K条数不同，尽可能绘出界面
@property (nonatomic , weak) UIScrollView *scrollerView;
@property (nonatomic, strong) NSArray *multipleSelectedTitles;

/**
 分时图数据源
 */
@property (nonatomic , strong) NSMutableArray *timeLineModelArray;
@property (nonatomic , weak) YDVerticalStockTimeLineView *timeLineView;

/**
 K线图数据源
 */
@property (nonatomic , strong) NSMutableArray *dayKLineModelsArray;
@property (nonatomic , strong) NSMutableArray *weekKLineModelsArray;
@property (nonatomic , strong) NSMutableArray *monthKLineModelsArray;
@property (nonatomic , weak) YDVerticalStockBlendKView *dayKview;
@property (nonatomic , weak) YDVerticalStockBlendKView *weekKview;
@property (nonatomic , weak) YDVerticalStockBlendKView *monthKview;
/**
 加载数据的菊花
 */
@property (nonatomic,weak) UIActivityIndicatorView *activityView;
@property (nonatomic , assign) BOOL isGestureActivity;
/**
 临时数据数组
 */
@property (nonatomic , strong) NSMutableArray *tempArray;

/**
 两个相同，不加载数据
 */
@property (nonatomic , strong) NSString *moreDataEndString;


//加载分时数据的定时器
@property (nonatomic , strong) NSTimer *timer;

@end

@implementation YDVerticalKTimeLineView

-(NSMutableArray *)timeLineModelArray
{
    if (_timeLineModelArray == nil) {
        _timeLineModelArray = [NSMutableArray array];
    }
    return _timeLineModelArray;
}
-(NSMutableArray *)dayKLineModelsArray
{
    if (_dayKLineModelsArray == nil) {
        _dayKLineModelsArray = [NSMutableArray array];
    }
    return _dayKLineModelsArray;
}
-(NSMutableArray *)weekKLineModelsArray
{
    if (_weekKLineModelsArray == nil) {
        _weekKLineModelsArray = [NSMutableArray array];
    }
    return _weekKLineModelsArray;
}
-(NSMutableArray *)monthKLineModelsArray
{
    if (_monthKLineModelsArray == nil) {
        _monthKLineModelsArray = [NSMutableArray array];
    }
    return _monthKLineModelsArray;
}

- (NSArray *)multipleSelectedTitles
{
    if (!_multipleSelectedTitles) {
        _multipleSelectedTitles = @[@" 分时 ", @" 日K " , @" 周K " , @" 月K "];
    }
    return _multipleSelectedTitles;
}
-(NSMutableArray *)tempArray
{
    if (_tempArray == nil) {
        _tempArray = [NSMutableArray array];
    }
    return _tempArray;
}

-(void)setStockName:(NSString *)stockName
{
    _stockName = stockName;
    if (!stockName)return;
}
-(void)setStockCode:(NSString *)stockCode
{
    _stockCode = stockCode;
    if (!stockCode.length)return;
    if (self.activityView.isAnimating)return;
    [self loadDataDidChangeStock];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _lineType = TimeLine;
        _moreDataEndString = @"";
        [self addScrollerView];
        [self addMultipleSelectedView];
        [self activityView];
        [self timeLineView];
    }
    return self;
}
+(instancetype)verticalKTimeLineViewInitWithFrame:(CGRect)frame WithStockName:(NSString *)stockName StockCode:(NSString *)stockCode
{
    YDVerticalKTimeLineView *KLTimeView = [[YDVerticalKTimeLineView alloc] initWithFrame:frame];
    KLTimeView.stockCode = stockCode;
    KLTimeView.stockName = stockName;
    return KLTimeView;
}
/**
 滚动视图
 */
-(void)addScrollerView
{
    CGFloat scrollerH = 0.0f;
    scrollerH = isPhoneX ? rateX812(34) : rate750(34);
    UIScrollView *scrollerView = [[UIScrollView alloc] init];
    scrollerView.frame = CGRectMake(0, 0, LKScreenW, scrollerH);
    scrollerView.backgroundColor = [UIColor whiteColor];
    scrollerView.scrollsToTop = NO;
    scrollerView.bounces = YES;
    [self addSubview:scrollerView];
    scrollerView.delegate = self;
    self.scrollerView = scrollerView;
    scrollerView.showsHorizontalScrollIndicator = NO;
    scrollerView.showsVerticalScrollIndicator = NO;
    scrollerView.contentSize = CGSizeMake(4* scrollerView.width, 0);
    scrollerView.pagingEnabled = YES;
}

/**
 多选择器
 */
-(void)addMultipleSelectedView
{
    MulTipleViewSelectedView * catergoryView = [MulTipleViewSelectedView new];
    catergoryView.edgeSpacing = rate375(20);
    catergoryView.titles = self.multipleSelectedTitles;
    catergoryView.scrollView = self.scrollerView;
    catergoryView.delegate = self;
    catergoryView.backgroundColor = [UIColor whiteColor];
    catergoryView.titleColor = [UIColor colorWithHexString:blueyGrey];
    catergoryView.titleSelectColor = [UIColor colorWithHexString:stockRed];
    catergoryView.titleColorChangeEable = YES;
    catergoryView.titleColorChangeGradually = YES;
    catergoryView.titleFont = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
    catergoryView.scrollWithAnimaitonWhenClicked = NO;
    catergoryView.backEllipseEable = NO;
    catergoryView.scaleEnable = YES;
    catergoryView.bottomLineEable = YES;
    catergoryView.bottomLineColor = [UIColor colorWithHexString:stockRed];
    catergoryView.bottomLineSpacingFromTitleBottom = 1;
    [self addSubview:catergoryView];
    [catergoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.scrollerView);
        make.left.right.equalTo(self);
    }];
}
/**
 监听多选择器点击
 */
- (void)catergoryView:(MulTipleViewSelectedView *)catergoryView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {//分时
        if (_lineType == TimeLine)return;
        _lineType = TimeLine;
        self.timeLineView.hidden = NO;
        if (_dayKview != nil) {
            [self.dayKview removeFromSuperview];
            self.dayKview = nil;
        }
        if (_weekKview != nil) {
            [self.weekKview removeFromSuperview];
            self.weekKview = nil;
        }
        if (_monthKview != nil) {
            [self.monthKview removeFromSuperview];
            self.monthKview = nil;
        }
        
    }else if (indexPath.item == 1){//日K
        if (_lineType == DayKLine)return;
        _lineType = DayKLine;
        _dayNumbers = 200;
        _KDataType = indexPath.item;
        self.timeLineView.hidden = YES;

        if (self.dayKview != nil) {
            self.dayKview.delegate = nil;
            [self.dayKview removeFromSuperview];
            self.dayKview = nil;
        }
        
        if (self.weekKview != nil) {
            self.weekKview.delegate = nil;
            [self.weekKview removeFromSuperview];
            self.weekKview = nil;
        }
        if (self.monthKview != nil) {
            self.monthKview.delegate = nil;
            [self.monthKview removeFromSuperview];
            self.monthKview = nil;
        }
        
        [self.dayKLineModelsArray removeAllObjects];
        [self.weekKLineModelsArray removeAllObjects];
        [self.monthKLineModelsArray removeAllObjects];
        
        [self dayKview];
        
        
        [self loadDayKLineData];
        
    }else if (indexPath.item == 2){//周K
        if (_lineType == WeekKLine)return;
        _lineType = WeekKLine;
        _KDataType = indexPath.item;
        _dayNumbers = 500;
        self.timeLineView.hidden = YES;

        if (self.dayKview != nil) {
            self.dayKview.delegate = nil;
            [self.dayKview removeFromSuperview];
            self.dayKview = nil;
        }
        if (self.weekKview != nil) {
            self.weekKview.delegate = nil;
            [self.weekKview removeFromSuperview];
            self.weekKview = nil;
        }
        if (self.monthKview != nil) {
            self.monthKview.delegate = nil;
            [self.monthKview removeFromSuperview];
            self.monthKview = nil;
        }
        
        [self.dayKLineModelsArray removeAllObjects];
        [self.weekKLineModelsArray removeAllObjects];
        [self.monthKLineModelsArray removeAllObjects];
        
        [self weekKview];
        
        [self loadWeekKLineData];
    }else if (indexPath.item == 3){//月K
        if (_lineType == MonthKLine)return;
        _lineType = MonthKLine;
        _KDataType = indexPath.item;
        _dayNumbers = 2000;
        self.timeLineView.hidden = YES;

        
        if (self.dayKview != nil) {
            self.dayKview.delegate = nil;
            [self.dayKview removeFromSuperview];
            self.dayKview = nil;
        }
        if (self.weekKview != nil) {
            self.weekKview.delegate = nil;
            [self.weekKview removeFromSuperview];
            self.weekKview = nil;
        }
        if (self.monthKview != nil) {
            self.monthKview.delegate = nil;
            [self.monthKview removeFromSuperview];
            self.monthKview = nil;
        }
        [self.dayKLineModelsArray removeAllObjects];
        [self.weekKLineModelsArray removeAllObjects];
        [self.monthKLineModelsArray removeAllObjects];
        
        
        [self monthKview];
        [self loadMonthKLineData];
    }
    
    
}
/*
 TimeLine = 1,
 DayKLine,
 WeekKLine,
 MonthKLine
 */
-(void)loadDataDidChangeStock
{
    if (_lineType ==  TimeLine) {
        [self loadTimeLineData];
    }else if (_lineType ==  DayKLine){
        [self loadDayKLineData];
    }else if (_lineType ==  WeekKLine){
        [self loadWeekKLineData];
    }else if (_lineType ==  MonthKLine){
        [self loadMonthKLineData];
    }
}

/**
分时
 */
-(YDVerticalStockTimeLineView *)timeLineView
{
    if (_timeLineView == nil) {
        YDVerticalStockTimeLineView *timeLineView = [[YDVerticalStockTimeLineView alloc] initWithTimeLineModels:self.timeLineModelArray];
        timeLineView.frame = CGRectMake(0, CGRectGetMaxY(self.scrollerView.frame), self.width, self.height - CGRectGetMaxY(self.scrollerView.frame));
        [self insertSubview:timeLineView belowSubview:self.activityView];
        timeLineView.backgroundColor = [UIColor whiteColor];
        timeLineView.hidden = YES;
        timeLineView.delegate = self;
        _timeLineView = timeLineView;
    }
    return _timeLineView;
}
-(void)verticalStockTimeLineViewGestureState:(SYVerticalStockTimeLineViewGestureState)gestureState
{
    if (gestureState == GestureStateActivity) {
//        [self stopTimer];
        self.isGestureActivity = YES;
    }else if (gestureState == GestureStateEnd){
//        [self openTimer];
        self.isGestureActivity = NO;
    }
}
/**
K图
 */
-(YDVerticalStockBlendKView *)dayKview
{
    if (_dayKview == nil) {
        YDVerticalStockBlendKView *dayKview = [[YDVerticalStockBlendKView alloc] initWithKLineModels:self.dayKLineModelsArray];
        dayKview.frame = CGRectMake(0, CGRectGetMaxY(self.scrollerView.frame), self.width, self.height - CGRectGetMaxY(self.scrollerView.frame));
        [self insertSubview:dayKview belowSubview:self.activityView];
        dayKview.delegate = self;
        _dayKview = dayKview;
    }
    return _dayKview;
}
-(YDVerticalStockBlendKView *)weekKview
{
    if (_weekKview == nil) {
        YDVerticalStockBlendKView *weekKview = [[YDVerticalStockBlendKView alloc] initWithKLineModels:self.weekKLineModelsArray];
        weekKview.frame = CGRectMake(0, CGRectGetMaxY(self.scrollerView.frame), self.width, self.height - CGRectGetMaxY(self.scrollerView.frame));
        [self insertSubview:weekKview belowSubview:self.activityView];
        weekKview.delegate = self;
        _weekKview = weekKview;
    }
    return _weekKview;
}
-(YDVerticalStockBlendKView *)monthKview
{
    if (_monthKview == nil) {
        YDVerticalStockBlendKView *monthKview = [[YDVerticalStockBlendKView alloc] initWithKLineModels:self.monthKLineModelsArray];
        monthKview.frame = CGRectMake(0, CGRectGetMaxY(self.scrollerView.frame), self.width, self.height - CGRectGetMaxY(self.scrollerView.frame));
        [self insertSubview:monthKview belowSubview:self.activityView];
        monthKview.delegate = self;
        _monthKview = monthKview;
    }
    return _monthKview;
}

/**
 菊花
 */
-(UIActivityIndicatorView *)activityView
{
    if (_activityView == nil) {
        UIActivityIndicatorView *activityView = [UIActivityIndicatorView new];
        [self addSubview:activityView];
        activityView.hidesWhenStopped = YES;
        activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        _activityView = activityView;
    }
    return _activityView;
}

/*********加载数据*********/

/**
 加载分时数据
 */
-(void)loadTimeLineData
{
    if (_isGestureActivity) {
        DDLog(@"又收拾， 直接返回吧")
        return;
    }
    if (_lineType != TimeLine)return;
    [self.timeLineModelArray removeAllObjects];
    [self.activityView startAnimating];
    NSString *url;
    if (!self.stockCode) {//股票代码为空
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"无数据"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        return;
    }else{
        url = [NSString stringWithFormat:@"%@%@",@"/stock/timeLine/" , self.stockCode];
    }
    NSString *baseUrl = [NSString stringWithFormat:@"%@%@",baseUrlHttp , url];
    NSString *authToken = [[SYUserDataManger sharedInstance] getUserToken];
    BaseNetRequest *request = [[BaseNetRequest alloc] initWithRequestMethod:GETMethod];
    [request sessionRequestWithDictionary:nil andURLString:baseUrl andAuthToken:authToken WithSuccessBlock:^(NSDictionary *resultDict) {
        DDLog(@"%@" , resultDict)
        [self.activityView stopAnimating];
        if ([resultDict[@"code"] integerValue] == 200) {
            
            [resultDict[@"result"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                StockTimeLineModel *model = [[StockTimeLineModel alloc]initWithDict:obj];
                
                [model upDateTimeLineLimitDataWithDataArray:resultDict[@"result"] Index:idx];
                
                [self.timeLineModelArray addObject: model];
                
                
            }];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.timeLineView removeFromSuperview];
                self.timeLineView = nil;
                [self timeLineView];
                if (_lineType == TimeLine) {
                    self.timeLineView.hidden = NO;
                }else{
                    self.timeLineView.hidden = YES;
                }
                [_timeLineView reDrawWithTimeLineModels:self.timeLineModelArray];
                
                [self openTimer];
                
                DDLog(@"self.timeLineModelArray = %zd" , self.timeLineModelArray.count)
                
            });

        }else{
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        }
    } failBlock:^(NSError *error) {
        [self.activityView stopAnimating];
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
    }];
    

    
}
/************加载K线数据************/

/**
 加载日K数据
 */
-(void)loadDayKLineData
{
    [self.activityView startAnimating];
    NSString *endDateString = [NSDate getNowDateStringYMD];
    NSArray *dateStringArray = [NSDate getFixedTimeDifferenceFieldFixDay:_dayNumbers FromDateString:endDateString];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (!self.stockCode) {
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"无数据"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        return;
    }else{
        [dict setObject:self.stockCode forKey:@"stockCode"];
    }
    [dict setObject:@(self.KDataType) forKey:@"type"];
    [dict setObject:dateStringArray.firstObject forKey:@"startTime"];
    [dict setObject:dateStringArray.lastObject forKey:@"endTime"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:GetKLineData WithSuccessBlock:^(NSDictionary *resultDict) {
        //        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            [self.activityView stopAnimating];
            //倒序
            NSMutableArray *dictArray = (NSMutableArray *)resultDict[@"result"];
            NSMutableArray *reverDictArray = (NSMutableArray *)[[dictArray reverseObjectEnumerator] allObjects];
            [reverDictArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                StockKLineModel *data = [[StockKLineModel alloc] initWithDict:obj];
                
                NSString *day = [NSString stringWithFormat:@"%@" , obj[@"day"]];
                if ([reverDictArray count] % 14 == ([reverDictArray indexOfObject:obj] + 1 )%14 ) {
                    
                    data.showDay = [NSString stringWithFormat:@"%@-%@-%@",[day substringToIndex:4],[day substringWithRange:NSMakeRange(5, 2)],[day substringWithRange:NSMakeRange(8, 2)]];
                }
                [self.dayKLineModelsArray addObject:data];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.dayKLineModelsArray.count < 60)return;
                if (_dayKview == nil)return;
                [self.dayKview reDrawWithKLineModelsArray:self.dayKLineModelsArray];
            });
        }else{
            [self.activityView stopAnimating];
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        }
        
    } failBlock:^(NSError *error) {
        [self.activityView stopAnimating];
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
    }];
}

/**
 加载周K数据
 */
-(void)loadWeekKLineData
{
    [self.activityView startAnimating];
    NSString *endDateString = [NSDate getNowDateStringYMD];
    NSArray *dateStringArray = [NSDate getFixedTimeDifferenceFieldFixDay:_dayNumbers FromDateString:endDateString];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (!self.stockCode) {
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"无数据"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        return;
    }else{
        [dict setObject:self.stockCode forKey:@"stockCode"];
    }
    [dict setObject:@(self.KDataType) forKey:@"type"];
    [dict setObject:dateStringArray.firstObject forKey:@"startTime"];
    [dict setObject:dateStringArray.lastObject forKey:@"endTime"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:GetKLineData WithSuccessBlock:^(NSDictionary *resultDict) {
        //        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            [self.activityView stopAnimating];
            //倒序
            NSMutableArray *dictArray = (NSMutableArray *)resultDict[@"result"];
            NSMutableArray *reverDictArray = (NSMutableArray *)[[dictArray reverseObjectEnumerator] allObjects];
            [reverDictArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                StockKLineModel *data = [[StockKLineModel alloc] initWithDict:obj];
                
                NSString *day = [NSString stringWithFormat:@"%@" , obj[@"day"]];
                if ([reverDictArray count] % 14 == ([reverDictArray indexOfObject:obj] + 1 )%14 ) {
                    
                    data.showDay = [NSString stringWithFormat:@"%@-%@-%@",[day substringToIndex:4],[day substringWithRange:NSMakeRange(5, 2)],[day substringWithRange:NSMakeRange(8, 2)]];
                }
                [self.weekKLineModelsArray addObject:data];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.weekKLineModelsArray.count < 60)return;
                if (_weekKview == nil)return;
                [self.weekKview reDrawWithKLineModelsArray:self.weekKLineModelsArray];
            });
           
        }else{
            [self.activityView stopAnimating];
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        }
        
    } failBlock:^(NSError *error) {
        [self.activityView stopAnimating];
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
    }];
}

/**
 加载月K数据
 */
-(void)loadMonthKLineData
{
    [self.activityView startAnimating];
    NSString *endDateString = [NSDate getNowDateStringYMD];
    NSArray *dateStringArray = [NSDate getFixedTimeDifferenceFieldFixDay:_dayNumbers FromDateString:endDateString];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (!self.stockCode) {
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"无数据"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        return;
    }else{
        [dict setObject:self.stockCode forKey:@"stockCode"];
    }
    [dict setObject:@(self.KDataType) forKey:@"type"];
    [dict setObject:dateStringArray.firstObject forKey:@"startTime"];
    [dict setObject:dateStringArray.lastObject forKey:@"endTime"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:GetKLineData WithSuccessBlock:^(NSDictionary *resultDict) {
        //        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            [self.activityView stopAnimating];
            //倒序
            NSMutableArray *dictArray = (NSMutableArray *)resultDict[@"result"];
            NSMutableArray *reverDictArray = (NSMutableArray *)[[dictArray reverseObjectEnumerator] allObjects];
            [reverDictArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                StockKLineModel *data = [[StockKLineModel alloc] initWithDict:obj];
                
                NSString *day = [NSString stringWithFormat:@"%@" , obj[@"day"]];
                if ([reverDictArray count] % 14 == ([reverDictArray indexOfObject:obj] + 1 )%14 ) {
                    
                    data.showDay = [NSString stringWithFormat:@"%@-%@-%@",[day substringToIndex:4],[day substringWithRange:NSMakeRange(5, 2)],[day substringWithRange:NSMakeRange(8, 2)]];
                }
                [self.monthKLineModelsArray addObject:data];
            }];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.monthKLineModelsArray.count < 60)return;
                if (_monthKview == nil)return;
                [self.monthKview reDrawWithKLineModelsArray:self.monthKLineModelsArray];
            });
            
        }else{
            [self.activityView stopAnimating];
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        }
        
    } failBlock:^(NSError *error) {
        [self.activityView stopAnimating];
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
    }];
}


-(void)stockKLineView:(YDVerticalStockBlendKView *)blendKLineView LoadmoreData:(NSString *)loadDataNeedString
{
    
    [self.tempArray removeAllObjects];
    if ([_moreDataEndString isEqualToString:loadDataNeedString]){
        if (_KDataType == 1) {
            
            [self.dayKview reLoadMoreKLineModelsArray:self.tempArray];
        }else if (_KDataType == 2){
            [self.weekKview reLoadMoreKLineModelsArray:self.tempArray];
        }else if (_KDataType == 3){
            [self.monthKview reLoadMoreKLineModelsArray:self.tempArray];
        }
        return;
    }
    _moreDataEndString = loadDataNeedString;
    
    [self.activityView startAnimating];
    NSString *endDateString = loadDataNeedString;
    NSArray *dateStringArray = [NSDate getFixedTimeDifferenceFieldFixDay:_dayNumbers FromDateString:endDateString];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (!self.stockCode) {
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"无数据"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
    }else{
        [dict setObject:self.stockCode forKey:@"stockCode"];
    }
    [dict setObject:@(self.KDataType) forKey:@"type"];
    [dict setObject:dateStringArray.firstObject forKey:@"startTime"];
    [dict setObject:dateStringArray.lastObject forKey:@"endTime"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:GetKLineData WithSuccessBlock:^(NSDictionary *resultDict) {
//        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            [self.activityView stopAnimating];
            //倒序
            NSMutableArray *dictArray = (NSMutableArray *)resultDict[@"result"];
            NSMutableArray *reverDictArray = (NSMutableArray *)[[dictArray reverseObjectEnumerator] allObjects];
            
            [reverDictArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                StockKLineModel *data = [[StockKLineModel alloc] initWithDict:obj];
                
                NSString *day = [NSString stringWithFormat:@"%@" , obj[@"day"]];
                if ([reverDictArray count] % 14 == ([reverDictArray indexOfObject:obj] + 1 )%14 ) {
                    
                    data.showDay = [NSString stringWithFormat:@"%@-%@-%@",[day substringToIndex:4],[day substringWithRange:NSMakeRange(5, 2)],[day substringWithRange:NSMakeRange(8, 2)]];
                }
                [self.tempArray addObject:data];
            }];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (_KDataType == 1) {
                    [self.dayKview reLoadMoreKLineModelsArray:self.tempArray];
                }else if (_KDataType == 2){
                    [self.weekKview reLoadMoreKLineModelsArray:self.tempArray];
                }else if (_KDataType == 3){
                    [self.monthKview reLoadMoreKLineModelsArray:self.tempArray];
                }
            });
            
        }else{
            [self.activityView stopAnimating];
            [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
            [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
            if (_KDataType == 1) {
                [self.dayKview reLoadMoreKLineModelsArray:self.tempArray];
            }else if (_KDataType == 2){
                [self.weekKview reLoadMoreKLineModelsArray:self.tempArray];
            }else if (_KDataType == 3){
                [self.monthKview reLoadMoreKLineModelsArray:self.tempArray];
            }
        }
        
    } failBlock:^(NSError *error) {
        [self.activityView stopAnimating];
        [[SYProgressHUD sharedInstance] showSVOnlytextWithText:@"数据加载出错"];
        [[SYProgressHUD sharedInstance] dismissSVWithTime:1];
        if (_KDataType == 1) {
            [self.dayKview reLoadMoreKLineModelsArray:self.tempArray];
        }else if (_KDataType == 2){
            [self.weekKview reLoadMoreKLineModelsArray:self.tempArray];
        }else if (_KDataType == 3){
            [self.monthKview reLoadMoreKLineModelsArray:self.tempArray];
        }
        
    }];
}


//定时器
-(void)openTimer
{
    if (self.timer)return;
    DDLog(@"开启定时器")
    self.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(loadTimeLineData) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)stopTimer
{
    if (!self.timer)return;
    DDLog(@"关闭定时器")
    [self.timer invalidate];
    self.timer = nil;
}

-(void)startTimer
{
    DDLog(@"定时器工作")
}

@end
